package com.example.demo.service.impl;

import com.example.demo.common.Result;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUser(Long id) {
        return userMapper.getById(id);
    }


    @Override
    public List<User> getUserList() {
        return userMapper.getAll();
    }
    @Override
    public Result<User> addUser(User user) {
        int count =userMapper.countByUsername(user.getUsername());
        if(count>0){
            return Result.error(400, "用户名已存在");
        }
        userMapper.insert(user);
        return Result.success();

    }

}
